package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.User;
import edu.neusoft.yuejian_09.model.UserModel;

public class LoginActivity extends Activity implements RetrofitListener<User> {
    private Button btn_reg,btn_login;//定义按钮
    private EditText username_login ,pass_login;//定义文本

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==RESULT_OK) {
            String username = data.getStringExtra("username");//从注册页面中得到值
            username_login.setText(username);
        }
    }
    public void initViews()//控件管理
    {
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_login = (Button) findViewById(R.id.btn_login);
        username_login =(EditText)findViewById(R.id.user_login);
        pass_login=(EditText)findViewById(R.id.pass_login);
    }

    public void initEvents()//事件管理
    {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();

            }


        });

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);
            }
        });
    }

    void login(){
        UserModel userModel=new UserModel();
        userModel.Login(username_login.getText().toString().trim(),pass_login.getText().toString().trim(),this);
    }


    @Override
    public void onSuccess(User userid,int flag){
        if(!"0".equals(userid.getUser_id())){
            saveUser(userid.getUser_id());
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

        }else {
            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFail(){ Toast.makeText(LoginActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}


    public void saveUser(int user_id){
        //将账号里和密码存入这
        SharedPreferences sharedPreferences =getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username",username_login.getText().toString());
        //int uid = Integer.parseInt(user_id);
        editor.putInt("user_id",user_id);
        editor.putString("userpass",pass_login.getText().toString());
        editor.commit();//提交修改
    }

}
